package com.onion.utils.utils;

import com.google.common.collect.Lists;

import java.util.*;

public class MapHelper {

    /**
     * 获取value
     * @param dataMap
     * @param key
     * @return
     */
    public static Object get(Map dataMap, String key){
        return dataMap.get(key);
    }

    /**
     * 获取字符串value
     * @param dataMap
     * @param key
     * @return
     */
    public static String getString(Map dataMap, String key){
        Object value = get(dataMap, key);
        if(value != null){
            if(value instanceof String){
                return (String)value;
            }else{
                return String.valueOf(value);
            }
        }
        return null;
    }

    /**
     * 获取Long型 value
     * @param dataMap
     * @param key
     * @return
     */
    public static Long getLong(Map dataMap, String key){
        Object value = get(dataMap, key);
        if(value != null){
            if(value instanceof Long){
                return (Long)value;
            }else if(value instanceof String){
                return Long.parseLong((String)value);
            }
        }
        return null;
    }

    /**
     * 获取整型 value
     * @param dataMap
     * @param key
     * @return
     */
    public static Integer getInteger(Map dataMap, String key){
        Object value = get(dataMap, key);
        if(value != null){
            if(value instanceof Integer){
                return (Integer) value;
            }else if(value instanceof String){
                return Integer.parseInt((String)value);
            }
        }
        return null;
    }

    /**
     * 获取Float value
     * @param dataMap
     * @param key
     * @return
     */
    public static Float getFloat(Map dataMap, String key){
        Object value = get(dataMap, key);
        if(value != null){
            if(value instanceof Float){
                return (Float) value;
            }else if(value instanceof String){
                return Float.parseFloat((String)value);
            }
        }
        return null;
    }

    /**
     * 获取Double value
     * @param dataMap
     * @param key
     * @return
     */
    public static Double getDouble(Map dataMap, String key){
        Object value = get(dataMap, key);
        if(value != null){
            if(value instanceof Double){
                return (Double) value;
            }else if(value instanceof String){
                return Double.parseDouble((String)value);
            }
        }
        return null;
    }

    /**
     * 获取排序后的Keys
     * @param dataMap
     * @return
     */
    public static List<String> getSortKeys(Map<String,?> dataMap){
        List<String> keys = Lists.newArrayList(dataMap.keySet());
        Collections.sort(keys);
        return keys;
    }

    /**
     * 按排序后的Keys获取value
     * @param dataMap
     * @return
     */
    public static List<String> getValuesBySortedKey(Map<String,String> dataMap){
        List<String> sortedKeys = getSortKeys(dataMap);
        List<String> sortedValues = new ArrayList<>(sortedKeys.size());
        for(int i=0; i<sortedKeys.size(); i++){
            sortedValues.add(dataMap.get(sortedKeys.get(i)));
        }
        return sortedValues;
    }

}